<template>
  <div class="app-container">

    <!-- 添加或修改车型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-collapse v-model="collapseController" >
          <el-collapse-item name="1">
            <template slot="title">
              <div style="font-size: 20px">车型信息</div>
            </template>

            <el-form-item label="选择所属品牌" prop="carBrandId">
              <el-select v-model="form.carBrandId" placeholder="请选择所属品牌" size="100%" @change="getSeriesList"
                         class="el-input-style" filterable remote>
              <el-option
                  v-for="(item,i) in brandList"
                  :key="item.brandId"
                  :label="item.brandName"
                  :value="item.brandId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="选择所属车系" prop="carSeriesId" >
              <el-select v-model="form.carSeriesId" placeholder="请选择所属车系"  filterable remote class="el-input-style">
                <el-option
                  v-for="(item,i) in seriesList"
                  :key="item.seriesId"
                  :label="item.seriesName"
                  :value="item.seriesId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="车型名称" prop="modelName">
              <el-input v-model="form.modelName" placeholder="请输入车型名称" class="el-input-style"/>
            </el-form-item>
          </el-collapse-item>
          <el-collapse-item name="2">
            <template slot="title">
              <div style="font-size: 20px">其他信息</div>
            </template>

            <el-row :gutter="20">
              <el-col :span="12">
                <div class="grid-content bg-purple">
                  <el-form-item label="车辆类型" prop="styleModel">
                    <el-select v-model="form.styleModel" placeholder="请选择车辆类型" class="el-input-style">
                      <el-option
                        v-for="dict in dict.type.style_model"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="grid-content bg-purple">
                  <el-form-item label="变速箱" prop="speedChangingBox">
                    <el-select v-model="form.speedChangingBox" placeholder="请选择变速箱" class="el-input-style">
                      <el-option
                        v-for="dict in dict.type.speed_changing_box"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <div class="grid-content bg-purple">
                  <el-form-item label="燃油类型" prop="fuelType">
                    <el-select v-model="form.fuelType" placeholder="请选择燃油类型" class="el-input-style">
                      <el-option
                        v-for="dict in dict.type.fuel_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="grid-content bg-purple">
                  <el-form-item label="排量" prop="displacement">
                    <el-select v-model="form.displacement" placeholder="请选择排量" class="el-input-style">
                      <el-option
                        v-for="dict in dict.type.displacement"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-form-item label="座位数" prop="seating">
              <!-- <el-input v-model="form.seating" placeholder="请输入座位数" class="el-input-style"/> -->
             <!-- <el-input-number v-model="form.seating" controls-position="right" :min="0" :step="1" :step-strictly="true"/> -->
						 <div class="grid-content bg-purple">
						   <el-select v-model="form.seating" placeholder="请选择座位" class="el-input-style">
						     <el-option
						       v-for="dict in dict.type.seat"
						       :key="dict.value"
						       :label="dict.label"
						       :value="parseInt(dict.value)"
						     ></el-option>
						   </el-select>
						 </div>
            </el-form-item>
          </el-collapse-item>
        </el-collapse>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .el-input-style{
    width: 300px;
  }
</style>

<script>
import {getModel, addModel, updateModel, getCarBrandList, getCarSeriesList} from "@/api/car/model";
import {getToken} from "@/utils/auth";

export default {
  name: "Model",
  dicts: ['style_model', 'speed_changing_box', 'displacement', 'fuel_type','seat'],
  data() {
    return {

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      brandList: [],
      seriesList: [],
      // 表单校验
      rules: {
        carBrandId: [{
          required: true,
          message: '请选择品牌',
          trigger: 'change'
        }],
        carSeriesId: [{
          required: true,
          message: '请选择车系',
          trigger: 'change'
        }],
        modelName: [{
          required: true,
          message: '请输入车型名称',
          trigger: 'blur'
        }],
        styleModel: [{
          required: true,
          message: '请选择车辆类型',
          trigger: 'blur'
        }],
      },
      collapseController: ['1','2'],
    };
  },
  created() {
    this.reset();
    getCarBrandList().then(response => {
      this.brandList = response.data;
    });
  },
  methods: {
    // 获取车系
    getSeriesList(carBrandId) {
      this.form.carSeriesId = null
      getCarSeriesList(carBrandId).then(response => {
        this.seriesList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carBrandId: null,
        carSeriesId: null,
        modelName: null,
        seating: null,
        styleModel: null,
        speedChangingBox: null,
        displacement: null,
        fuelType: null,
        delFlag: null,
        isAvailable: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.title = "添加车型";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getModel(id).then(response => {
        this.seriesList = response.data.seriesList;
        this.form = response.data.carmodel;
        this.open = true;
        this.title = "修改车型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateModel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("close");
            });
          } else {
            addModel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("close");
            });
          }
        }
      });
    },
  }
};
</script>
